import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:o2oa_all_platform/common/extension/date_extension.dart';

import '../../../common/models/im/index.dart';
import '../../../common/widgets/index.dart';
import 'index.dart';
import 'widgets/conversation_list_view.dart';


typedef OnTapConversationForBigScreen = void Function(IMConversationInfo info);

class ImPage extends GetView<ImController> {
  const ImPage({Key? key, this.onTapBigScreen}) : super(key: key);
  final OnTapConversationForBigScreen? onTapBigScreen;

  // 主视图
  Widget _buildView(BuildContext context) {
    return  Obx(() => RefreshIndicator(
            onRefresh: () => controller.loadConversationList(),
            child: Column(
              children: [
                Visibility(
                    visible: controller.state.instantMsg.value != null,
                    child: lastInstantMsgItemView(context)),
                Visibility(
                    visible: controller.state.instantMsg.value != null,
                    child: const SizedBox(height: 1)),
                Expanded(
                    flex: 1,
                    child: ConversationListView(
                      controller.state.conversationList.toList(),
                      selectedId: controller.state.selectedConversationId.value,
                      onTapConversation: (info) {
                        controller.state.selectedConversationId.value = info.id; // 选中
                        if (onTapBigScreen != null) {
                          onTapBigScreen!(info);
                          return;
                        }
                        controller.tapOpenConversation(info);
                      }
                    ))
              ],
            )));
  }

  ///
  /// 系统消息
  ///
  Widget lastInstantMsgItemView(BuildContext context) {
    return Obx(() {
      InstantMsg? msg = controller.state.instantMsg.value;
      String lastTime = '';
      String subTitle = '';
      if (msg?.createTime != null) {
        DateTime? time = DateTime.tryParse(msg?.createTime ?? "");
        if (time != null) {
          lastTime = time.friendlyTime();
        }
        subTitle = msg?.title ?? '';
      }
      return Container(
          decoration: BoxDecoration(
            // borderRadius: const BorderRadius.all(Radius.circular(10)),
            color: Theme.of(context).colorScheme.surface,
          ),
          child: ListTile(
            leading: const SizedBox(
              width: 50,
              height: 50,
              child: AssetsImageView("icon_msg.png"),
            ),
            title: Text(
              'im_instant_msg_title'.tr,
              softWrap: true,
              textAlign: TextAlign.left,
              overflow: TextOverflow.ellipsis,
              maxLines: 1,
              style: Theme.of(context).textTheme.bodyLarge,
            ),
            subtitle: Text(
              subTitle,
              softWrap: true,
              textAlign: TextAlign.left,
              overflow: TextOverflow.ellipsis,
              maxLines: 1,
              style: Theme.of(context).textTheme.bodyMedium,
            ),
            trailing:
                Text(lastTime, style: Theme.of(context).textTheme.bodySmall),
            onTap: () => controller.clickInstant(),
          ));
    });
  }

  @override
  Widget build(BuildContext context) {
    return GetBuilder<ImController>(
      builder: (_) {
        return Scaffold(
          appBar: AppBar(
            title: Text('home_tab_im'.tr),
            actions: [menu()],
          ),
          body: SafeArea(
              child: Container(
            color: Theme.of(context).colorScheme.surface,
            child: _buildView(context),
          )),
          floatingActionButton: Obx(() => Visibility(
                visible: controller.state.showSpeechAssistant,
                child: FloatingActionButton(
                    onPressed: () => controller.startSpeechAssistant(),
                    backgroundColor: Theme.of(context).colorScheme.primary,
                    child: const AssetsImageView('icon_zhinengyuyin.png',
                        width: 48, height: 48)
                    // const Icon(
                    //   Icons.record_voice_over,
                    //   color: Colors.white,
                    //   size: 36,
                    // )
                    ),
              )),
        );
      },
    );
  }

  Widget menu() {
    return Obx(() {
      final list = controller.state.menuList.toList();
      return PopupMenuButton<int>(
        icon: const Icon(Icons.add_circle_outline, color: Colors.white),
        itemBuilder: (context) => list,
        onSelected: (value) {
          switch (value) {
            case 0:
              controller.startChat(onTapBigScreen: onTapBigScreen);
              break;
            // case 1:
            //   controller.startGroupChat();
            // break;
            case 3:
              controller.tap2Collection();
              break;
            default:
              break;
          }
        },
      );
    });
  }
}
